/*
 * Copyright 2012 Dominik.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.PA165.musiclibraryweb.genre;

import cz.muni.fi.PA165.musiclibraryapi.dto.GenreDTOInterface;
import cz.muni.fi.PA165.musiclibraryapi.dto.SongDTOInterface;
import cz.muni.fi.PA165.musiclibraryapi.services.GenreService;
import cz.muni.fi.PA165.musiclibraryapi.services.SongService;
import cz.muni.fi.PA165.musiclibrarybackend.dto.GenreDTO;
import cz.muni.fi.PA165.musiclibraryweb.ForAdminBookmarkableLink;
import cz.muni.fi.PA165.musiclibraryweb.ForAdminLink;
import cz.muni.fi.PA165.musiclibraryweb.HomePage;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.AbstractItem;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author Dominik
 */
@AuthorizeInstantiation({"USER", "ADMIN"})
public class GenrePage extends HomePage implements Serializable {

    @SpringBean
    private GenreService service;
    private Label errorMessage;

    public GenrePage(final PageParameters parameters) {
        this(parameters, null);
    }

    //Error while deleting
    public GenrePage(final PageParameters parameters, Label label) {
        super(parameters);
        add(new BookmarkablePageLink("genreView", GenrePage.class));
        
        BookmarkablePageLink bookmarkLink = new ForAdminBookmarkableLink("genreAdd", AddGenrePage.class);

        add(bookmarkLink);

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        final SongService songService = context.getBean(SongService.class);
        service = context.getBean(GenreService.class);

        List<GenreDTOInterface> genres;
        if (service != null) {
            genres = service.getAllGenres();
        } else {
            genres = new ArrayList<GenreDTOInterface>();
        }
        //Show some error message
        if (label == null) {
            errorMessage = new Label("error", "");
            errorMessage.setVisible(false);
        } else {
            errorMessage = label;
            errorMessage.setVisible(true);
        }
        add(errorMessage);

        RepeatingView repeating = new RepeatingView("repeating");
        for (GenreDTOInterface a : genres) {
            AbstractItem item = new AbstractItem(repeating.newChildId());

            repeating.add(item);
            final GenreDTO g = new GenreDTO();
            g.setName(a.getName());
            g.setId(a.getId());
            item.add(new Label("name", a.getName()));
            final Link editLink = new ForAdminLink("edit") {
                @Override
                public void onClick() {
                    AddGenrePage addGenrePage = new AddGenrePage(parameters, g);
                    setResponsePage(addGenrePage);
                }
            };

            item.add(editLink);
            final Link deleteLink = new ForAdminLink("delete") {
                @Override
                public void onClick() {
                    GenrePage genrePage = null;
                    boolean exist = false;
                    for (SongDTOInterface s : songService.getAllSongs()) {
                        if ((s.getGenre().equals(g))) {
                            exist = true;
                            Label label = new Label("error",
                                    ("Genre " + g.getName() + ", is still used by song " + s.getName() + "."));
                            genrePage = new GenrePage(parameters, label);
                            break;
                        }
                    }
                    if (!exist) {
                        try {
                            service.deleteGenre(g);
                            genrePage = new GenrePage(parameters);
                        } catch (Exception exception) {
                            setResponsePage(new GenrePage(parameters,
                                    new Label("error", "Database error!")));
                        }
                    }
                    setResponsePage(genrePage);
                }
            };

            item.add(deleteLink);
        }
        add(repeating);
    }
}
